package SQL.abysnSQL;

import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;

import fatworm.sql.grammar.ColName;
import fatworm.sql.grammar.ConstValue;

/*
 * value ::= (value) | col-name | const-value | value op value
			 | (subquery with only one elements (or NULL) in the result table)
 */

public class Value {
	public Value value;
	public ColName colname;
	public ConstValue constvalue;
	public Value l,r;
	public Aop aop;
	public SQL subquery;
	public TYPE type;
	
	public static enum TYPE{
		SUBQUERY,COLNAME,VALUE,CONST,AOPEXPR
	}
	
	public int valueType;
	
	public static final int INTEGER = 1;
	public static final int FLOAT = 2;
	public static final int STRING = 0;
	public static final int COLUMN = -1;
	
	public Value(Value v){
		value = v;
		type = TYPE.VALUE;
		valueType = v.valueType;
	}
	
	public Value(ColName cn){
		colname = cn;
		type = TYPE.COLNAME;
		valueType = COLUMN;
	}
	
	public Value(ConstValue cv){
		this.constvalue = cv;
		if(constvalue instanceof IntegerValue){
			valueType = INTEGER;
		}
		else if(constvalue  instanceof FloatValue){
			valueType = FLOAT;
		}
		else{
			valueType = STRING;
		}
	}
	
	public Value(Value l, Aop op, Value r){
		this.l = l;
		this.aop = op;
		this.r = r;
		type = TYPE.AOPEXPR;
		switch(l.valueType){
		case INTEGER:
			valueType = INTEGER;
			break;
		case FLOAT:
			valueType = FLOAT;
			break;
		case STRING:
			valueType = STRING;
		}
	}
	
	public Value(SQL s){
		subquery = s;
		type = TYPE.SUBQUERY;
	}
	
	//TODO
	
	public int valueType(){
		return valueType;
	}
	
	public String toString() {
		switch (type) {
		case COLNAME:
			return colname.toString();
		case CONST:
			return constvalue.toString();
		case AOPEXPR:
			return l.toString()+aop.toString()+r.toString();
		case SUBQUERY:
			return subquery.toString();
		default:
			return value.toString();
		}
	}
	
	public ColName getColName() {
		switch(type) {
		case COLNAME:
			return colname;
		case VALUE:
			return value.getColName();
		}
		return null;
	}
	
	public ConstValue getConstValue() {
		switch(type) {
		case CONST:
			return constvalue;
		case VALUE:
			return value.constvalue;
		}
		return null;
	}
}
